﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Accounting;

namespace School.Pages.Accounting
{
    public partial class DiscountReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(104) == true)
                {
                    if (!IsPostBack)
                    {
                        LstCampuses.DataSource = Structure.GetCampuses();
                        LstCampuses.DataTextField = "Name";
                        LstCampuses.DataValueField = "ID";
                        LstCampuses.DataBind();

                        LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                        LstCertificates.DataValueField = "Id";
                        LstCertificates.DataTextField = "Name";
                        LstCertificates.DataBind();

                        ListBox1.DataSource = Expense.GetAllExpenses();
                        ListBox1.DataValueField = "ID";
                        ListBox1.DataTextField = "Name";
                        ListBox1.DataBind();

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
                        LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
                        lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });

                        Session["DiscountReportSource"] = null;
                        Session["IncomeReportTotal"] = null;
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });            
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });            
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });            
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataSource = Structure.GetClassesByGrade(long.Parse(LstGrades.SelectedValue));
            lstClasses.DataValueField = "Id";
            lstClasses.DataTextField = "Name";
            lstClasses.DataBind();
            lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });
        }

        protected void Button1_Click(object sender, EventArgs e)
        {            
            long Campus = 0;
            long Certificate = 0;
            long Stage = 0;
            long Grade = 0;
            int Class = 0;
            List<long> expenses = new List<long>();

            DateTime fromDate = new DateTime(0001,1,1);
            DateTime toDate = new DateTime(9999,12,31);

            if (TxtFrom.Text != "")
            {
                fromDate = DateTime.Parse(TxtFrom.Text);
            }
            if (TxtTo.Text != "")
            {
                toDate = DateTime.Parse(TxtTo.Text);
                toDate.AddHours(23);
                toDate.AddMinutes(59);
                toDate.AddSeconds(59);
            }

            Campus = long.Parse(LstCampuses.SelectedValue);
            Certificate = long.Parse(LstCertificates.SelectedValue);
            Stage = long.Parse(LstStages.SelectedValue);
            Grade = long.Parse(LstGrades.SelectedValue);
            Class = int.Parse(lstClasses.SelectedValue);

            foreach (ListItem i in ListBox1.Items)
            {
                if (i.Selected)
                    expenses.Add(long.Parse(i.Value));
            }

            var v = Discount.getAppliedDiscounts(expenses, fromDate, toDate, Campus, Certificate, Stage, Grade, Class);

            GridView1.DataSource = v;
            GridView1.DataBind();            

            if (GridView1.Rows.Count > 0)
            {
                LblTotal.Text = v.Sum(x => x.DiscountValue).ToString();
                Panel1.Visible = true;
                Session["DiscountReportSource"] = v;
                Session["DiscountReportTotal"] = LblTotal.Text;
            } 
        }

        protected void lstClasses_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}